package com.hmkj.core.api.ufx.model.res;

import com.xiaoleilu.hutool.date.DateUtil;
import com.xiaoleilu.hutool.util.StrUtil;
import lombok.Data;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * 期货成交回报
 *
 * @author Vector
 * @create 2017-11-14 16:08
 */
@Data
public class DealPush {
    /**
     * 目标机构号
     */
    private String companyId;
    /**
     * 发送者机构号
     */
    private String senderId;
    /**
     * 委托编号
     */
    private String futuEntrustNo;
    /**
     * 成交编号
     */
    private String futuBusinessNo;
    /**
     * 买卖方向
     */
    private String entrustBs;
    /**
     * 交易类别
     */
    private String futuExchType;
    /**
     * 品种代码类别
     */
    private String commodityType;
    /**
     * 合约代码
     */
    private String contractCode;
    /**
     * 资产账户
     */
    private String fundAccount;
    /**
     * 委托状态
     */
    private String futuEntrustStatus;
    /**
     * 成交价格
     */
    private BigDecimal futuBusinessPrice;
    /**
     * 成交数量
     */
    private int businessAmount;
    /**
     * 交易日期
     */
    private String initDate;
    /**
     * 成交时间
     */
    private String businessTime;
    /**
     * 委托数量
     */
    private String entrustAmount;
    /**
     * 委托引用
     */
    private String entrustReference;
    /**
     * 成交时间
     */
    private Date dealTime;
    /**
     * 处理过期时间
     */
    private Date dealOutTime;

    // msg_type|company_id|sender_id|futu_entrust_no|entrust_bs|futu_exch_type|commodity_type|contract_code|options_type|strike_price|
    // futu_business_no|futu_business_price|business_amount|fund_account|init_date|business_time|
    public static DealPush buildData(Map<String, String> datas,String topic) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        DealPush dp = new DealPush();

        if ("futu.trade_push".equals(topic)) {
            // 内盘期货
            dp.setCompanyId(datas.get("company_id"));
            dp.setSenderId(datas.get("sender_id"));
            dp.setFutuEntrustNo(datas.get("entrust_no"));
            dp.setEntrustBs(datas.get("entrust_bs"));
            dp.setFutuExchType(datas.get("futu_exch_type"));
            dp.setCommodityType(datas.get("commodity_type"));
            dp.setContractCode(datas.get("contract_code").replace(dp.getCommodityType(),""));
            dp.setFutuBusinessNo(datas.get("business_id"));
            dp.setFutuBusinessPrice(StrUtil.isBlank(datas.get("futu_business_price")) ? BigDecimal.ZERO : new BigDecimal(datas.get("futu_business_price")));
            dp.setBusinessAmount(StrUtil.isBlank(datas.get("business_amount")) ? 0 : Integer.parseInt(datas.get("business_amount")));
            dp.setFundAccount(datas.get("fund_account"));
            dp.setInitDate(datas.get("init_date"));
            dp.setBusinessTime(datas.get("business_time"));
            dp.setEntrustAmount(datas.get("entrust_amount"));
            dp.setEntrustReference(datas.get("entrust_reference"));
            dp.setDealTime(format.parse(dp.getInitDate() + dp.getBusinessTime()));
            dp.setDealOutTime(DateUtil.offsetMinute(new Date(), 3));
        }else{
            // 外盘期货
            dp.setCompanyId(datas.get("company_id"));
            dp.setSenderId(datas.get("sender_id"));
            dp.setFutuEntrustNo(datas.get("futu_entrust_no"));
            dp.setEntrustBs(datas.get("entrust_bs"));
            dp.setFutuExchType(datas.get("futu_exch_type"));
            dp.setCommodityType(datas.get("commodity_type"));
            dp.setContractCode(datas.get("contract_code"));
            dp.setFutuBusinessNo(datas.get("futu_business_no"));
            dp.setFutuBusinessPrice(StrUtil.isBlank(datas.get("futu_business_price")) ? BigDecimal.ZERO : new BigDecimal(datas.get("futu_business_price")));
            dp.setBusinessAmount(StrUtil.isBlank(datas.get("business_amount")) ? 0 : Integer.parseInt(datas.get("business_amount")));
            dp.setFundAccount(datas.get("fund_account"));
            dp.setInitDate(datas.get("init_date"));
            dp.setBusinessTime(datas.get("business_time"));
            dp.setEntrustAmount(datas.get("entrust_amount"));
            dp.setEntrustReference(datas.get("entrust_reference"));
            dp.setDealTime(format.parse(dp.getInitDate() + dp.getBusinessTime()));
            dp.setDealOutTime(DateUtil.offsetMinute(new Date(), 3));
        }
        return dp;
    }
}
